﻿
Imports System.IO
Imports Framework
Imports mshtml
Imports System.Web.Script.Serialization   ' verweis nötig auf system.web.extension
Imports System.Runtime.Serialization.Json ' verweis nötig auf System.ServiceModel.Web und System.Runtime.Serialization

Public Class frmMain
    Private _conn As MySQL
    Private _loc As UInt32 = 3
    Private _frmLoginData As frmDatabaseLogin

#Region " Form"

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        _frmLoginData = New frmDatabaseLogin
        Do While IsNothing(_frmLoginData.LoginData)
            If _frmLoginData.ShowDialog = Windows.Forms.DialogResult.OK Then
                Exit Do
            End If
        Loop
        Do While _frmLoginData.LoginData.isServerTested = False
            If _frmLoginData.ShowDialog = Windows.Forms.DialogResult.OK Then
                Exit Do
            End If
        Loop
        _conn = _frmLoginData.SQL
        ShowStatusZeile()
    End Sub
    Private Sub Ende()
        If IsNothing(_conn) = False Then
            _conn.Close()
        End If
        Application.Exit()
    End Sub
    Private Sub EndeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EndeToolStripMenuItem.Click
        Ende()
    End Sub
    Private Sub BenutzerToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BenutzerToolStripMenuItem.Click
        If _frmLoginData.ShowDialog = Windows.Forms.DialogResult.OK Then
            _conn = _frmLoginData.SQL
            ShowStatusZeile()
        End If
    End Sub
    Private Sub ShowStatusZeile()
        ToolStripStatusLabel1.Text = "Nicht verbunden    "
        ToolStripStatusLabel2.Text = ""
        ToolStripStatusLabel3.Text = ""
        If IsNothing(_conn) Then Exit Sub
        If _conn.IsConnected Then
            ToolStripStatusLabel1.Text = "Verbunden   "
            ToolStripStatusLabel2.Text = _conn.ConnectedTo & "   "
            ToolStripStatusLabel3.Text = _conn.SelectedDatabase & "   "
        End If
    End Sub
    Private Sub frmMain_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Select Case TabControlSearch.SelectedTab.Name
            Case Is = "TabPageQuestTemplate"
                Dim x As UInt32 = (ListViewQuestTemplate.Width - 90) \ 2
                If x < 100 Then x = 100
                ListViewQuestTemplate.Columns.Item(1).Width = x
                ListViewQuestTemplate.Columns.Item(2).Width = x
            Case Is = "TabControlCreatureTemplate"
            Case Is = "TabPageCreature"
            Case Is = "TabPageGossipMenu"
            Case Is = "TabPageWaypointData"
            Case Is = "TabPageItemTemplate"
            Case Is = "TabPageGameObjectTemplate"
            Case Is = "TabPageGameObject"

            Case Else
                Debug.Print(TabControlSearch.SelectedTab.Name)
                Stop

        End Select
    End Sub

#End Region

#Region " QuestTemplate"

    Private WithEvents _QuestTemplate As frmQuestTemplate
    Private _ListOfQuestTemplate As New SortedList(Of UInt32, cQuestTemplate)

    Private Sub NumericBoxQuestID_KeyPress(sender As Object, e As KeyPressEventArgs) Handles NumericBoxQuestID.KeyPress
        If e.KeyChar = vbCr Then
            Dim id As UInt32 = sender.value
            Dim o As cQuestTemplate = Nothing
            If _ListOfQuestTemplate.ContainsKey(id) Then
                o = _ListOfQuestTemplate.Item(id)
            Else
                o = New cQuestTemplate(_conn, id)
                _ListOfQuestTemplate.Add(id, o)
            End If
            _QuestTemplate = New frmQuestTemplate(o)
        End If
    End Sub
    Private Sub TextBoxTitle_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBoxTitle.KeyPress
        Stop
    End Sub
    Private Sub NumericBoxQuestGiver_KeyPress(sender As Object, e As KeyPressEventArgs) Handles NumericBoxQuestGiver.KeyPress
        Stop
    End Sub
    Private Sub NumericBoxQuestTaker_KeyPress(sender As Object, e As KeyPressEventArgs) Handles NumericBoxQuestTaker.KeyPress
        Stop
    End Sub
    Private Sub ListViewQuestTemplate_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ListViewQuestTemplate.MouseDoubleClick
        Stop
    End Sub
    Private Sub ListViewQuestTemplate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListViewQuestTemplate.SelectedIndexChanged
        Stop
    End Sub

#End Region

#Region " CreatureTemplate"

#End Region

#Region " Creature"

#End Region

#Region " GossipMenu"

#End Region

#Region " Import WoWHead Drops"

    Private Sub WoWHeadDropsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DropsToolStripMenuItem.Click
        Dim npc As UInt32 = InputBox("NPC entry:", "Eingabe der NPC-ID für die der loot gesucht wird", 55869)
        Using sw As New StreamWriter("drops_" & npc & ".sql", False, New System.Text.UTF8Encoding)
            sw.WriteLine("DELETE FROM creature_loot_template WHERE entry=" & npc & ";")
            Dim o As New MyWowheadReader
            Dim m_jss1 As New JavaScriptSerializer()
            Dim a As ArrayList = o.GetScriptElementsFromUrl("http://www.wowhead.com/npc=" & npc)
            For Each ie As mshtml.IHTMLElement In a
                Dim v1() As String = Split(ie.innerHTML, "new Listview(")
                For Each JSON As String In v1
                    If InStr(JSON, "template: 'item'") > 0 Then
                        JSON = MyWowheadReader.CleanJavaString(JSON)
                        Dim dict As Dictionary(Of String, Object) = Nothing
                        Try
                            dict = m_jss1.Deserialize(Of Dictionary(Of String, Object))(JSON)
                            Dim oData As ArrayList = dict.Item("data")
                            Dim count As UInt32 = dict.Item("_totalCount")
                            For Each o1 As Dictionary(Of String, Object) In oData
                                Dim Id As UInt32 = o1.Item("id")
                                Dim Percent As UInt32 = (100 / count) * o1.Item("count")
                                sw.WriteLine("INSERT INTO creature_loot_template VALUES (" & npc & "," & Id & "," & Percent & ",1,0,1,1);")
                            Next
                        Catch ex As Exception
                            Stop
                        End Try
                    End If
                Next
            Next
        End Using
    End Sub

#End Region
 
#Region " Assistenten"

#Region " Teleporter"

    Private Sub NPCToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NPCToolStripMenuItem.Click
        Dim frm As New frmAssistNpcTransporter(_conn, _loc)
        If frm.ShowDialog() Then

        End If
    End Sub
    Private Sub GameobjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GameobjectToolStripMenuItem.Click

    End Sub

#End Region


#End Region
   
   
  
End Class


